import pandas as pd
import os


def read_case(case_file):
    model = '所属模块'
    child_model = '子模块'
    auto_test_case_num = '自动化用例编号'
    prodect_name = '产品名称'
    case_title = '用例标题'
    q_version = '是否Q版'
    col_name = [model, child_model,
                auto_test_case_num, prodect_name, case_title]
    if os.path.isfile(case_file) and os.path.exists(case_file):
        df = pd.read_excel(case_file)
        data = {}
        default_model = []
        t = []
        q_version_row = df[q_version].to_list()
        q_version_col = []
        for i in range(0, len(q_version_row)):
            if q_version_row[i] == '是':
                q_version_col.append(i)

        case_title_row = df[case_title].to_list()
        case_title_col = []
        for i in range(0, len(case_title_row)):
            if pd.isnull(df.loc[i, case_title]):
                pass
            else:
                case_title_col.append(i)
        correct_case = list(
            set(q_version_col).intersection(set(case_title_col)))

        for idx, row in df.iterrows():
            if str(df.loc[idx, model]) == '/':
                if pd.isnull(df.loc[idx, child_model]):
                    if idx in correct_case:
                        default_model.append(idx)
                        data.update({"其他": default_model})
            else:
                if pd.isnull(df.loc[idx, child_model]):
                    t.append(idx)
                else:
                    df.loc[idx, child_model]
                    if idx in correct_case:
                        data.update({str(df.loc[idx, model])+'-' +
                                    str(df.loc[idx, child_model]): [idx]})
        for i in t:
            if pd.isnull(df.loc[i, child_model]):
                if i in correct_case:
                    data.update({df.loc[i, model]: []})

        for k, v in data.items():
            for i in correct_case:
                if k == df.loc[i, model]:
                    v.append(i)
        if data:
            return data
        else:
            return False
